/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hancadpo.client.view;

import br.com.sales.generics.client.components.autocomplete.widget.Autocomplete;
import br.com.sales.generics.client.components.mask.MaskEditHandler;
import br.com.sales.generics.client.components.scroll.HScroll;
import br.com.sales.generics.client.components.timepicker.HDateTimePicker;
import br.com.sales.generics.client.utils.numbers.DecimalBox;
import br.com.sales.hancadpo.client.presenter.HanCadPOPresenter;
import br.com.sales.hancadpo.client.presenter.HanCadPOUiHanlder;
import br.com.sales.generics.client.presenter.events.ShowLoadingEvent;
import br.com.sales.generics.client.presenter.events.ShowLoadingHandler;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.CalendarUtil;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import java.util.Date;

/**
 *
 * @author Karine
 */
public class HanCadPOView extends ViewWithUiHandlers<HanCadPOUiHanlder>
        implements HanCadPOPresenter.Display,
        ShowLoadingHandler {

    private static final HanCadPOViewUiBinder uiBinder = GWT.create(HanCadPOViewUiBinder.class);
    private final HScroll mainContentScrollPanel = new HScroll();
    @UiField
    FlowPanel mainContentPanel;
    @UiField
    HTMLPanel container;
    @UiField
    TextBox describeProductEdit;
    @UiField
    MaskEditHandler valueUnitEdit;
    @UiField
    MaskEditHandler valueBuyEdit;
    @UiField
    MaskEditHandler valuePromotionalEdit;
    @UiField
    DecimalBox profitMarginEdit;
    @UiField
    DecimalBox countStockEdit;
    @UiField
    CheckBox isStockChec;
    @UiField
    DecimalBox countMinStockEdit;
    @UiField
    DecimalBox countMaxStockEdit;
    @UiField
    TextBox dateEdit;
    @UiField
    TextArea describeEdit;
    @UiField
    VerticalPanel providerEdit;
    @UiField
    Button saveBtn;
    @UiField
    Button newBtn;
    @UiField
    Button cancelBtn;
    @UiField
    Button gridDetailBtn;
    @UiField
    HTMLPanel detailPanel;
    @UiField
    Image loadingMessage;
    @UiField
    HDateTimePicker initialDate;
    @UiField
    HDateTimePicker finalDate;
    @UiField
    Image refreshBtn;
    @UiField
    TextBox filterName;
    @UiField
    HTMLPanel filterContainer;
    private Autocomplete providerComplete;
    @Inject
    private EventBus eventBus;

    @Inject
    public HanCadPOView() {
        container = uiBinder.createAndBindUi(this);
    }

    @Override
    public void build() {
        valueBuyEdit.addStyleName("decimal");
        valuePromotionalEdit.addStyleName("decimal");
        valueUnitEdit.addStyleName("decimal");
        addEventHandler();
        loadingMessage.setVisible(false);
        mainContentScrollPanel.setStyleName("container-product-scroll", true);
        providerComplete = Autocomplete.build()
                .setAttributeNameDesc("NM_PESSOA_FISICA")
                .setAttributeNameID("NR_SEQUENCIA")
                .setIsMultiValued(false)
                .setTableName("PESSOA_FISICA");
        providerEdit.add(providerComplete);
        setStatus();
        adjustMaxLength();
        addOnClickButton();
        setDateFilter();
        refreshBtn.setResource(HanCadPOResources.INSTANCE.refresh());
    }

    private void setDateFilter() {
        DateTimeFormat dtf = DateTimeFormat.getFormat("dd/MM/yyyy");
        Date data = new Date(System.currentTimeMillis());
        finalDate.setValue(dtf.format(data));
        CalendarUtil.addDaysToDate(data, -5);
        initialDate.setValue(dtf.format(data));
    }

    private void adjustMaxLength() {
        describeProductEdit.setMaxLength(255);
        countMaxStockEdit.setMaxLength(4);
        countMinStockEdit.setMaxLength(4);
        countStockEdit.setMaxLength(4);
        profitMarginEdit.setMaxLength(4);
    }

    private void addEventHandler() {
        eventBus.addHandler(ShowLoadingEvent.TYPE, this);
    }

    @UiHandler("saveBtn")
    public void onClickSave(ClickEvent event) {
        getUiHandlers().onClickSave(event);
    }

    @UiHandler("newBtn")
    public void onClickNew(ClickEvent event) {
        getUiHandlers().onClickNew(event);
    }

    @UiHandler("cancelBtn")
    public void onClickCancel(ClickEvent event) {
        getUiHandlers().onClickCancel(event);
    }

    @UiHandler("gridDetailBtn")
    public void onClickGridDetail(ClickEvent event) {
        getUiHandlers().onClickGridDetail(event);
    }

    @Override
    public boolean isMandatory(boolean isEditing) {
        boolean isMandatory = false;
        if (("".equals(describeProductEdit.getText())
                || describeProductEdit.getText() == null)
                && isEditing) {
            describeProductEdit.getElement().addClassName("mandatory");
            isMandatory = true;
        } else {
            describeProductEdit.getElement().removeClassName("mandatory");
        }
        if (("".equals(valueUnitEdit.getValue())
                || valueUnitEdit.getValue() == null)
                && isEditing) {
            valueUnitEdit.getElement().addClassName("mandatory");
            isMandatory = true;
        } else {
            valueUnitEdit.getElement().removeClassName("mandatory");
        }
        if (("".equals(countStockEdit.getValue())
                || countStockEdit.getValue() == null)
                && isEditing) {
            countStockEdit.getElement().addClassName("mandatory");
            isMandatory = true;
        } else {
            countStockEdit.getElement().removeClassName("mandatory");
        }
        return isMandatory;
    }

    @Override
    public void setEnabled(boolean isNewrecord) {
        controlVisibleButton(isNewrecord);
        setFieldsEnabled(isNewrecord);
    }

    public void setFieldsEnabled(boolean enabled) {
        describeProductEdit.setEnabled(enabled);
        valueUnitEdit.setEnabled(enabled);
        valueBuyEdit.setEnabled(enabled);
        valuePromotionalEdit.setEnabled(enabled);
        profitMarginEdit.setEnabled(enabled);
        isStockChec.setEnabled(enabled);
        countStockEdit.setEnabled(enabled);
        countMinStockEdit.setVisible(enabled);
        countMaxStockEdit.setEnabled(enabled);
        providerComplete.setEnabled(enabled);
        describeEdit.setEnabled(enabled);
    }

    @Override
    public void controlVisibleButton(boolean isNewrecord) {
        cancelBtn.setVisible(isNewrecord);
        saveBtn.setVisible(isNewrecord);
        newBtn.setVisible(!isNewrecord);
        gridDetailBtn.setVisible(!isNewrecord);
    }

    private void setMainContentScroll(IsWidget content) {
        mainContentScrollPanel.clear();
        mainContentPanel.clear();

        if (content != null) {
            mainContentScrollPanel.add(content);
            mainContentPanel.add(mainContentScrollPanel);
        }
    }

    @Override
    public void setInSlot(Object slot, IsWidget content) {
        if (slot == HanCadPOPresenter.HANCADPO_SLOT_TYPE) {
            setMainContentScroll(content);
        }
    }

    @Override
    public void showLoading(boolean show) {
        loadingMessage.setVisible(show);
    }

    private void setStatus() {
        getDateEdit().setEnabled(false);
    }

    @Override
    public Widget asWidget() {
        return container;
    }

    @Override
    public HTMLPanel getContainer() {
        return container;
    }

    @Override
    public TextBox getDescribeProductEdit() {
        return describeProductEdit;
    }

    @Override
    public MaskEditHandler getValueUnitEdit() {
        return valueUnitEdit;
    }

    @Override
    public MaskEditHandler getValueBuyEdit() {
        return valueBuyEdit;
    }

    @Override
    public MaskEditHandler getValuePromotionalEdit() {
        return valuePromotionalEdit;
    }

    @Override
    public DecimalBox getProfitMarginEdit() {
        return profitMarginEdit;
    }

    @Override
    public DecimalBox getCountStockEdit() {
        return countStockEdit;
    }

    @Override
    public CheckBox getIsStockChec() {
        return isStockChec;
    }

    @Override
    public DecimalBox getCountMinStockEdit() {
        return countMinStockEdit;
    }

    @Override
    public DecimalBox getCountMaxStockEdit() {
        return countMaxStockEdit;
    }

    @Override
    public TextArea getDescribeEdit() {
        return describeEdit;
    }

    @Override
    public TextBox getDateEdit() {
        return dateEdit;
    }

    @Override
    public HTMLPanel getDetailPanel() {
        return detailPanel;
    }

    @Override
    public Autocomplete getProviderEdit() {
        return providerComplete;
    }

    @Override
    public HScroll getMainContentScrollPanel() {
        return mainContentScrollPanel;
    }

    @Override
    public Image getLoadingMessage() {
        return loadingMessage;
    }

    @Override
    public HDateTimePicker getInitialDate() {
        return initialDate;
    }

    public void setInitialDate(HDateTimePicker initialDate) {
        this.initialDate = initialDate;
    }

    @Override
    public HDateTimePicker getFinalDate() {
        return finalDate;
    }

    public void setFinalDate(HDateTimePicker finalDate) {
        this.finalDate = finalDate;
    }

    @Override
    public TextBox getFilterName() {
        return filterName;
    }

    public void setFilterName(TextBox filterName) {
        this.filterName = filterName;
    }

    public Image getRefreshBtn() {
        return refreshBtn;
    }

    public void setRefreshBtn(Image refreshBtn) {
        this.refreshBtn = refreshBtn;
    }

    @Override
    public HTMLPanel getFilterContainer() {
        return filterContainer;
    }

    private void addOnClickButton() {
        refreshBtn.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                getUiHandlers().onClickRefresh(event);
            }
        });
    }

    interface HanCadPOViewUiBinder extends UiBinder<HTMLPanel, HanCadPOView> {
    }
}
